﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Salon
{
    class CarService
    {
        public CarService() { }

        public Car saveCar(Car car)
        {
            String insertQuery = "INSERT INTO Car VALUES(@mark,@type,@color,@year,@cost,@price,@salon,@discount)";

            String updateQuery = "UPDATE Car SET mark=@mark,type=@type,color=@color,year=@year,cost=@cost,price=@price,salon=@salon,discount=@discount WHERE id=@id";

            SqlCommand cmd = SqlService.getInstance().getCommand( (car.Id < 1) ? insertQuery : updateQuery );
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@mark", car.Mark);
            cmd.Parameters.AddWithValue("@type", car.Type);
            cmd.Parameters.AddWithValue("@color", car.Color);
            cmd.Parameters.AddWithValue("@year", car.Year);
            cmd.Parameters.AddWithValue("@cost", car.Cost);
            cmd.Parameters.AddWithValue("@price", car.Price);
            cmd.Parameters.AddWithValue("@salon", car.Salon);
            cmd.Parameters.AddWithValue("@discount", car.Discount);
            if ( car.Id > 0 )
                cmd.Parameters.AddWithValue("@id", car.Id);
            cmd.ExecuteNonQuery();

            return car;
        }

        //przy usuwaniu ustawiamy flagę active na false
        public void deleteCar(Car Car)
        {
            String query = "DELETE FROM Car WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", Car.Id);
            cmd.ExecuteNonQuery();
        }

        //sprawdza czy dany klient już istnieje i zwraca id
        public int checkIfNameExists(String car_name)
        {
            String query = "Select id FROM Car WHERE name=@clien_name";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@clien_name", car_name);
            String id = (String)cmd.ExecuteScalar();

            if (id != null) return Convert.ToInt32(id);
            return 0;
        }

        public Car getCarById(int id)
        {
            String query = "SELECT * from Car WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataReader rdr = cmd.ExecuteReader();
            if (!rdr.HasRows)
                return null;
            rdr.Read();

            Car Car   = new Car();
            Car.Id    = rdr.GetInt32(rdr.GetOrdinal("id"));
            Car.Mark  = rdr.GetInt32(rdr.GetOrdinal("mark")) ;
            Car.Type  = rdr.GetInt32(rdr.GetOrdinal("type"));
            Car.Color = rdr.GetInt32(rdr.GetOrdinal("color"));
            Car.Year = rdr.GetInt32(rdr.GetOrdinal("year"));
            Car.Cost = rdr.GetFloat(rdr.GetOrdinal("cost"));
            Car.Price = rdr.GetFloat(rdr.GetOrdinal("price"));
            Car.Discount = rdr.GetFloat(rdr.GetOrdinal("discount"));
            Car.Salon = rdr.GetInt32(rdr.GetOrdinal("salon"));

            return Car;
        }

        /// <summary>
        /// Zwraca listę obiektów typu Car na podstawie listy kontrolek filtrujących
        /// </summary>
        /// <param name="filterList"></param>
        /// <returns></returns>
        public IList<Car> getCars(IList<IFilter> filterList)
        {
            IList<Car> Cars = new List<Car>();
            String query = "SELECT * FROM Car WHERE id > 0 ";
            query += Global.getConditionString(filterList);

            SqlDataReader rdr = SqlService.getInstance().getCommand(query).ExecuteReader();
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    Car Car = new Car();
                    Car.Id = rdr.GetInt32(rdr.GetOrdinal("id"));
                    Car.Mark = rdr.GetInt32(rdr.GetOrdinal("mark"));
                    Car.Type = rdr.GetInt32(rdr.GetOrdinal("type"));
                    Car.Color = rdr.GetInt32(rdr.GetOrdinal("color"));
                    Car.Year = rdr.GetInt32(rdr.GetOrdinal("year"));
                    Car.Cost = rdr.GetFloat(rdr.GetOrdinal("cost"));
                    Car.Price = rdr.GetFloat(rdr.GetOrdinal("price"));
                    Car.Discount = rdr.GetFloat(rdr.GetOrdinal("discount"));
                    Car.Salon = rdr.GetInt32(rdr.GetOrdinal("salon"));

                    Cars.Add(Car);
                }
            }

            rdr.Close();

            return Cars;
        }
    }
}
